<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="导览标题" prop="guideTitle">
        <el-input
          v-model="queryParams.guideTitle"
          placeholder="请输入导览标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="导航类型" prop="guideType">
        <el-select v-model="queryParams.guideType" placeholder="请选择导航类型" clearable>
          <el-option
            v-for="dict in guideTypeList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="guideStatus">
        <el-select v-model="queryParams.guideStatus" placeholder="请选择状态" clearable>
          <el-option
          v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['baseplatform:tours:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['baseplatform:tours:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['baseplatform:tours:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="toursList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="guideId" />
      <el-table-column label="导览标题" align="center" prop="guideTitle" />
      <el-table-column label="导航类型" align="center" prop="guideType">
        <template slot-scope="scope">
          <div v-for="(item,index) in guideTypeList">
            <span v-if="scope.row.guideType == item.value">{{item.label}}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="guideStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.guideStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['baseplatform:tours:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['baseplatform:tours:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改导览信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="导航类型" prop="guideType">
          <el-select v-model="form.guideType" placeholder="请选择导航类型" @change="loadLinkList">
            <el-option
              v-for="dict in guideTypeList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关联">
          <el-select v-model="form.linkId" placeholder="请选择关联" @change="linkChange" clearable>
            <el-option
              v-for="dict in linkList"
              :key="dict.id"
              :label="dict.label"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="导览标题" prop="guideTitle">
          <el-input v-model="form.guideTitle" placeholder="请输入导览标题" />
        </el-form-item>
        <el-form-item label="导航简介" prop="guideInfo">
          <el-input v-model="form.guideInfo" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <MapStd :containerId="'toursMap'" @selected="mapSelected" :longitude="form.guideLongitude" :latitude="form.guideLatitude"></MapStd>
        <el-form-item label="经度" prop="guideLongitude">
          <el-input v-model="form.guideLongitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="维度" prop="guideLatitude">
          <el-input v-model="form.guideLatitude" placeholder="请输入维度" />
        </el-form-item>
        <el-form-item label="图片路径" prop="imagePath">
          <image-upload v-model="form.imagePath" :limit="1"/>
        </el-form-item>
        <el-form-item label="语音路径" prop="soundPath">
          <fileUpload v-model="form.soundPath" :limit=1 :fileSize=200 :fileType="['mp3','wma','m4a']"></fileUpload>
        </el-form-item>
        <el-form-item label="视频路径" prop="videoPath">
          <fileUpload v-model="form.videoPath" :limit=1 :fileSize=200 :fileType="null"></fileUpload>
        </el-form-item>
        <el-form-item label="状态" prop="guideStatus">
          <el-radio-group v-model="form.guideStatus">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTours, getTours, delTours, addTours, updateTours } from "@/api/baseplatform/tours";
import { listType } from "@/api/baseplatform/type";
import { listContent } from "@/api/baseplatform/content";
import { listRestaurant } from "@/api/shop/restaurant";
import { listHotel } from "@/api/shop/hotel";
import { listStore } from "@/api/shop/store";

export default {
  name: "Tours",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 导览信息表格数据
      toursList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        guideTitle: null,
        guideType: null,
        guideStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      //导航分类
      guideTypeList:[],
      //关联文章文创等
      linkList:[],
    };
  },
  created() {
    this.getList();
    this.getGuideTypeList();
  },
  methods: {
    /** 查询导览信息列表 */
    getList() {
      this.loading = true;
      listTours(this.queryParams).then(response => {
        this.toursList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        guideId: null,
        guideTitle: null,
        guideType: null,
        guideInfo: null,
        guideLongitude: null,
        guideLatitude: null,
        soundPath: null,
        videoPath: null,
        linkId: null,
        guideStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.guideId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加导览信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const guideId = row.guideId || this.ids
      getTours(guideId).then(response => {
        this.form = response.data;
        this.loadLinkList();
        this.open = true;
        this.title = "修改导览信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.guideId != null) {
            if(this.form.imagePath && this.form.imagePath instanceof Array){
              let imageInfo = this.form.imagePath[0];
              this.form.imageZoomPath = imageInfo.zoomUrl;
              this.form.imagePath = imageInfo.url;
            }
            updateTours(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            if(this.form.imagePath && this.form.imagePath instanceof Array){
              let imageInfo = this.form.imagePath[0];
              this.form.imageZoomPath = imageInfo.zoomUrl;
              this.form.imagePath = imageInfo.url;
            }
            addTours(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const guideIds = row.guideId || this.ids;
      this.$modal.confirm('是否确认删除导览信息编号为"' + guideIds + '"的数据项？').then(function() {
        return delTours(guideIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('baseplatform/tours/export', {
        ...this.queryParams
      }, `tours_${new Date().getTime()}.xlsx`)
    },
    getGuideTypeList(){
      listType().then(response =>{
        this.guideTypeList = [];
        for(let i in response.rows) {
          this.guideTypeList.push({value:response.rows[i].guidedTypeId, label:response.rows[i].guidedTypeName})
        }
      });
    },
    //加载都有哪些文章 文创 酒店 ...
    loadLinkList() {
      this.linkList = [];
      if(this.form.guideType == 1) {
        //景点
        listContent({channelId:"8"}).then(response => {
          let contentList = response.rows;
          for(let i in contentList) {
            this.linkList.push(
              {
                id:""+contentList[i].id,
                guideInfo:contentList[i].introduction,
                label:contentList[i].title,
                longitude:contentList[i].longitude,
                latitude:contentList[i].latitude,
              }
            );
          }
        });
      }
      if(this.form.guideType == 3) {
        //卫生间
      }
      if(this.form.guideType == 2) {
        //停车场
      }
      if(this.form.guideType == 5) {
        //餐饮
        listRestaurant().then(response => {
          let contentList = response.rows;
          for(let i in contentList) {
            this.linkList.push(
              {
                id:""+contentList[i].id,
                label:contentList[i].name,
                guideInfo:contentList[i].notice,
                longitude:contentList[i].longitude,
                latitude:contentList[i].latitude,
              }
            );
          }
        })
      }
      if(this.form.guideType == 6) {
        //酒店
        listHotel().then(response => {
          let contentList = response.rows;
          for(let i in contentList) {
            this.linkList.push(
              {
                id:""+contentList[i].id,
                label:contentList[i].name,
                longitude:contentList[i].longitude,
                latitude:contentList[i].latitude,
              }
            );
          }
        })
      }
      if(this.form.guideType == 4) {
        //文创
        listStore().then(response => {
          let contentList = response.rows;
          for(let i in contentList) {
            this.linkList.push(
              {
                id:""+contentList[i].id,
                label:contentList[i].name,
                guideInfo:contentList[i].storeDesc,
                longitude:contentList[i].longitude,
                latitude:contentList[i].latitude,
              }
            );
          }
        })
      }
    },
    //带出相关信息
    linkChange(){
      for(let i in this.linkList) {
        if(this.linkList[i].id == this.form.linkId){
          this.form.guideTitle = this.linkList[i].label;
          this.form.guideInfo = this.linkList[i].guideInfo;
          this.form.guideLongitude = this.linkList[i].longitude;
          this.form.guideLatitude = this.linkList[i].latitude;
        }
      }
    },
    mapSelected(point) {
      this.form.guideLatitude = point.lat;
      this.form.guideLongitude = point.lng;
    }
  }
};
</script>
